package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by jojo
 * @Date 2022/2/26
 * @Description
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService{
    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.校验参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdSensitive sensitive = this.getById(id);
        if (sensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据库不存在该数据");
        }
        //2.执行删除业务
        this.removeById(id);
        //3.响应数据
        return ResponseResult.okResult();
    }

    /**
     * 分页查询敏感词
     * @param sensitiveDTO
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(SensitiveDTO sensitiveDTO) {
        //1.校验参数
        if (sensitiveDTO==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //校验分页参数
        sensitiveDTO.checkParam();
        Page<AdSensitive> page = new Page<>(sensitiveDTO.getPage(),sensitiveDTO.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sensitiveDTO.getName())) {
            wrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        //执行分页业务
        IPage<AdSensitive> iPage = this.page(page, wrapper);
        return new PageResponseResult(sensitiveDTO.getPage(),
                sensitiveDTO.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 添加敏感词
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult addSensitive(AdSensitive sensitive) {
        //校验参数 敏感词不为空
        if (sensitive==null||StringUtils.isBlank(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives,sensitive.getSensitives());
        int count = this.count(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已经存在");
        }
        //执行新增
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateBySensitive(AdSensitive sensitive) {
        //校验参数 敏感词不为空
        if (sensitive==null||StringUtils.isBlank(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives,sensitive.getSensitives());
        int count = this.count(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"要修改的敏感词已经存在");
        }
        //执行修改业务
        updateById(sensitive);
        //响应参数
        return ResponseResult.okResult();
    }

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    /**
     * 查询所有敏感词
     * @return
     */
    @Override
    public ResponseResult<List<String>> findByList() {
        List<String> list= adSensitiveMapper.findByList();
        return ResponseResult.okResult(list);
    }
}
